local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CommonOHOH =_G.CommonOHOH
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local ConnectClass = _G.CommonOHOH.load.Connect();
local LanguageHelper=_G.LanguageHelper 
local TweenHelper = ClientOHOH.load.TweenHelper();


function InitInsts(ui)
	ui.insts = {};
	ui.insts.Cancel = "ctrl.btn.no";
	ui.insts.Confirm = "ctrl.btn.yes";

	ui.insts.SelfReady = "ctrl.self.ready";
	ui.insts.TargetReady = "ctrl.other.ready";

	ui.insts.WaitFinish = "ctrl.countdown";

	ui.insts.SelfName = "ctrl.self.name";
    ui.insts.TargetName ="ctrl.other.name";

    ui.insts.SelfHadList =  "ctrl.self.petRoot";
    ui.insts.TargetHadList = "ctrl.other.petRoot";
    
    ui.insts.SelfTradeList = "ctrl.self.putBox";
    ui.insts.TargetTradeList =  "ctrl.other.putBox";

end


function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.TradeGui"));

    self.insts.SelfReady.Text=LanguageHelper:GetText(10113) 
    self.insts.TargetReady.Text=LanguageHelper:GetText(10113) 

	self.insts.SelfName.Text=game.Players.LocalPlayer.Name

    self.CancelAction=ConnectClass.new()
    self.ReadyAction=ConnectClass.new()
    self.ChangeAction=ConnectClass.new()

    CallBackManager:AddRbxConnect(self,self.insts.Cancel.Activated, function()
		if self.UpdateFinishTimeConnect then
			self.UpdateFinishTimeConnect:Disconnect()
			self.UpdateFinishTimeConnect=nil
		end
        self.CancelAction:Action()
		self:Hide()
	end);

    CallBackManager:AddRbxConnect(self,self.insts.Confirm.Activated, function()
        self.ReadyAction:Action()
		self.insts.SelfReady.Visible=true
        self.insts.Confirm.Visible= false
	end);

	self.selfPetCellList={}
    self.targetPetCellList={}

end;


function M.dtor(self)
	if self.UpdateFinishTimeConnect then
		self.UpdateFinishTimeConnect:Disconnect()
		self.UpdateFinishTimeConnect=nil
	end
	M.super.dtor(self);
end;


function M.SetDataBase(self,dataBase)
	self.DataBase=dataBase
end


function M.GetDataBase(self)
    return self.DataBase
end

function M.FullCell(self,data,cellParent)
    local list={}
    for pid,count in pairs(data) do
		for i=1,count do
            local guid=pid.."_"..i
            local cell=ClientOHOH.load.PetCell().new(pid,guid,cellParent,self:GetDataBase())
			cell.main_inst.Name=guid
            table.insert(list,cell)
		end
    end
    return  list
end

function M.UpdateTradeData(self,data,tradeRoot,hadRoot)
	for _,cell in pairs(tradeRoot:GetChildren()) do
		if cell:IsA("Frame") then
			cell.Parent=hadRoot
		end
	end

	-- print("-----UpdateTradeData---A--")
	-- for _,v in pairs(hadRoot:GetChildren()) do
	-- 	print("had:",v.Name)
	-- end

	-- print("-----UpdateTradeData---B--")

	for petId,count in pairs(data) do

		for i=1,count do
			local id=petId.."_"..i
			local t=hadRoot:FindFirstChild(id)
			assert(t,"PetCell Is Null:"..id)
			t.Parent=tradeRoot
		end

	end
end
function M.UpdateTargetTrade(self,data)
	self:UpdateTradeData(data,self.insts.TargetTradeList,self.insts.TargetHadList)
	-- 对方已修改数据,取消准备
	self.insts.SelfReady.Visible=false
	self.insts.Confirm.Visible= true
	self.insts.TargetReady.Visible=false
end

function M.TargetReady(self)
	self.insts.TargetReady.Visible=true
end

local game_cfg = ClientOHOH.config.Game
function M.ReadyFinish(self)
	self.insts.TargetReady.Visible=true
	self.insts.SelfReady.Visible=true
	if not self.UpdateFinishTimeConnect then
		self.finishTime=game_cfg.TradeTime
		self.UpdateFinishTimeConnect=self:AddStepUpdate(self.UpdateFinishTime,0.05);
	end
end

function M.UpdateFinishTime(self)
	self.finishTime=self.finishTime-0.05
	self.insts.WaitFinish.Text=string.format("%.2f", self.finishTime)
	if self.finishTime<0 then
		self:Hide()
		self.insts.WaitFinish.Text=0
		if self.UpdateFinishTimeConnect then
			self.UpdateFinishTimeConnect:Disconnect()
			self.UpdateFinishTimeConnect=nil
		end
	end
end


function M.UpdateSelfTrade(self,data)
	self:UpdateTradeData(data,self.insts.SelfTradeList,self.insts.SelfHadList)
end

function M.ClearCellList(self)
	for k,v in pairs(self.selfPetCellList) do
		v:dtor()
	end
	for k,v in pairs(self.targetPetCellList) do
		v:dtor()
	end
	self.selfPetCellList={}
	self.targetPetCellList={}
end

function M.OnHide(self) 
	self:ClearCellList()
end

function M.OnShow(self,name,SelfData,TargetData,...) 

	self.insts.SelfReady.Visible=false
	self.insts.TargetReady.Visible=false
	self.insts.Confirm.Visible= true

	self.insts.TargetName.Text=name
	self.insts.WaitFinish.Text=""
	self:ClearCellList()
	self.Sum=0
	self.selfPetCellList=self:FullCell(SelfData,self.insts.SelfHadList)
    for _,cell in pairs(self.selfPetCellList) do
        cell.ClickEvent:Connect(function()
            self:MyCellClick(cell)
        end)
    end
	self.targetPetCellList=self:FullCell(TargetData,self.insts.TargetHadList)

	-- print("---------OnShow-------self.targetPetCellList----")
	-- for k,cell in pairs(self.targetPetCellList) do
	-- 	print("cell.main_inst.Name--->",cell.main_inst.Name)
	-- end
end;


function M.MyCellClick(self,cell) 
    if cell:GetParent()==self.insts.SelfHadList then --在等待区 
        if self.UpdateFinishTimeConnect then
                return
        end
        self.Sum=self.Sum+1
        if self.Sum>6 then
            self.Sum=6
            return
        end
        self.insts.TargetReady.Visible=false

        self.insts.SelfReady.Visible=false
        self.insts.Confirm.Visible= true

		self:AddProcess(function()
			self.ChangeAction:Action(cell:GetPetId(),1)--添加
		end)
       
        cell:SetParent(self.insts.SelfTradeList)
    else --在交易区
        if self.UpdateFinishTimeConnect then
            return
        end
        self.Sum=self.Sum-1
        if self.Sum<0 then
            self.Sum=0
        end
        self.insts.TargetReady.Visible=false
        self.insts.SelfReady.Visible=false
        self.insts.Confirm.Visible= true
		self:AddProcess(function()
			self.ChangeAction:Action(cell:GetPetId(),0) --移除
		end)
      
        cell:SetParent(self.insts.SelfHadList)
    end

end
return M
